// 云函数入口文件
const cloud = require("wx-server-sdk");
cloud.init();
const db = cloud.database();
const $ = db.command;

// 查找相应的用户数据
exports.main = async (event, context) => {
    if (!event.hasOwnProperty("query")) {
        return { err: "query error", desc: "请添加 query 字段" };
    }
    // ! 权限：调用人必为 Manager
    const { result: InvokerIsManager } = await cloud.callFunction({
        name: "isManager",
        data: {
            UserOpenId: event.userInfo.openId,
        },
    });
    if (InvokerIsManager !== true)
        return {
            err: "permission denied.\n" + JSON.stringify(InvokerIsManager),
            desc: "没有权限",
        };
    const gen = new db.RegExp({
        regexp: event.query,
        options: "i",
    });

    return await db
        .collection("User")
        .where(
            $.or([
                {
                    appId: gen,
                },
                {
                    nickName: gen,
                },
            ])
        )
        .get();
};
